package com.jianyi.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

public class Resource extends Model<Resource>{

	private static final long serialVersionUID = 5930133582587564793L;
	public static final Resource dao = new Resource();
	
	public List<Record> getResourcesTop(){
		String sql="select * from t_resource t where 1=1 and t.parentId is null order by sort";
		return Db.find(sql);
	}
	
	public Record getResourceForTree(Record record){
		String sql="select * from t_resource t where 1=1 and t.parentId=? order by sort";
		List<Record> children=Db.find(sql,record.getStr("id"));
		for (Record record2 : children) {
			getResourceForTree(record2);
		}
		record.set("children", children);
		return record;
	}
	
	public List<Record> getAllResourcesForTree(){
		List<Record> list=getResourcesTop();
		for (Record record : list) {
			getResourceForTree(record);
		}
		return list;
	}

}
